package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.repository.StrategyCommentRepository;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {

    @Autowired
    private StrategyCommentRepository repository;
    @Autowired
    private MongoTemplate template;

    @Override
    public void save(StrategyComment strategyComment) {
        //保存前将评论id设为空,防止在bean属性复制操作中将源对象id带过存到目标对象中.
        strategyComment.setId(null);
        //设置创建时间
        strategyComment.setCreateTime(new Date());
        repository.save(strategyComment);
    }

    @Override
    public void update(StrategyComment strategyComment) {
        repository.save(strategyComment);
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public StrategyComment get(String id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public List<StrategyComment> list() {
        return repository.findAll();
    }

    @Override
    public Page<StrategyComment> query(StrategyCommentQuery qo) {
        Query query = new Query();
        if (StringUtils.hasLength(qo.getStrategyId())){
            query.addCriteria(Criteria.where("strategyId").is(qo.getStrategyId()));
        }
        //当前页显示数据
        Pageable pageable = PageRequest.of(qo.getCurrentPage()-1,qo.getPageSize()
                , Sort.Direction.ASC,"_id");

        return DBHelper.query(query,template,StrategyComment.class,pageable,qo);
    }

    @Override
    public void saveOrUpdate(StrategyComment strategyComment) {
        if (StringUtils.hasLength(strategyComment.getId())){
            this.update(strategyComment);
        }else {
            this.save(strategyComment);
        }
    }

    @Override
    public void commentThumb(String cid, String uid) {
        //获取到该条评论的对象
        StrategyComment comment = this.get(cid);
        //获取评论点赞用户id集合
        List<String> thumbuplist = comment.getThumbuplist();
        //判断用户id是否在thumbuplist中
        if (thumbuplist.contains(uid)){
            //如果存在,表示该评论被该用户点赞过,当前请求为取消点赞,点赞数-1,用户id从thumbuplist移除
            comment.setThumbupnum(comment.getThumbupnum() - 1);
            thumbuplist.remove(uid);
        }else {
            //如果不存在,表示该评论未被该用户点赞过,当前请求为取消点赞,点赞数+1,用户id从thumbuplist添加
            comment.setThumbupnum(comment.getThumbupnum() + 1);
            thumbuplist.add(uid);
        }
        repository.save(comment);
    }
}
